﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using common;

namespace common.web
{
    public class upload
    {
        string fil, valn;  //文件类型选择,赋值对象名
        public upload()
        {
            fil = func.Request("fil");
            valn = func.Request("valn");
        }

        public void delwj()
        {
            string wj = func.Request("wj");
            if (wj != "") File.Delete(HttpContext.Current.Server.MapPath("../picfile/") + wj);
            string pt = HttpContext.Current.Request.Url.AbsolutePath;
            if (fil != "" || valn != "") pt += "?fil=" + fil + "&valn=" + valn;
            HttpContext.Current.Response.Redirect(pt);
        }

        public string wjsc(FileUpload fu, int fiz, string fin)   //fin文件名前缀
        {
            string _fiext = fiext(); //文件类型限制
            string nfilext = Path.GetExtension(fu.FileName).ToLower();
            if (!fu.HasFile)
            {
                HttpContext.Current.Response.Write("<script type=\"text/javascript\">parent.showzt('" + valn + "_zt','')</script>");
                func.Alert("文件为空或文件大小为0KB!", null);
            }
            else if (ckFilExt(_fiext, nfilext) == false)
            {
                HttpContext.Current.Response.Write("<script type=\"text/javascript\">parent.showzt('" + valn + "_zt','')</script>");
                func.Alert("文件类型不符合要求!", null);
            }
            else if (fu.PostedFile.ContentLength > fiz * 1024)
            {
                HttpContext.Current.Response.Write("<script type=\"text/javascript\">parent.showzt('" + valn + "_zt','')</script>");
                func.Alert("文件大小超出了限制,最大为" + fiz + "KB!", null);
            }
            else
            {
                string wj = inc.makdir("../picfile/"); //创新日期文件夹
                Random random = new Random();
                wj += fin + DateTime.Now.ToString("yyMMddHHmm") + random.Next(100, 900).ToString() + nfilext;
                fu.SaveAs(HttpContext.Current.Server.MapPath("../picfile/") + wj);
                string url = "";
                if (fil != "" || valn != "") url = "fil=" + fil + "&valn=" + valn + "&";
                HttpContext.Current.Response.Write("<script type=\"text/javascript\">parent.document.getElementById('" + valn + "').value='" + wj + "';");
                HttpContext.Current.Response.Write("parent.showzt('" + valn + "_zt','')</script>");
                return "<table width=\'100%\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'><tr><td height=\'22\'>文件上传成功!<a href=\"../picfile/" + wj + "\" target=\"_blank\"><font color=\"#FF0000\">[查看文件]</font></a>&nbsp;你也可以[<a href=\"?" + url + "del=yes&wj=" + wj + "\">删除</a>]后重传文件.</td></tr></table>";
            }
            return "";
        }

        public string fiext()
        {
            if (fil == "") return ".jpg;.gif;.bmp;.png";
            if (fil == "1") return ".doc;.docx;.pdf;.rar;.zip";
            return ".swf";
        }

        bool ckFilExt(string fExt, string fExt2)  //可用扩展名,当前扩展名
        {
            string[] fexts = fExt.Split(';');
            for (int i = 0; i < fexts.Length; i++)
            {
                if (fExt2 == fexts[i]) return true;
            }
            return false;
        }
    }
}